\hypertarget{ftp__client_8c}{}\doxysection{ftp\+\_\+client.\+c File Reference}
\label{ftp__client_8c}\index{ftp\_client.c@{ftp\_client.c}}
{\ttfamily \#include \char`\"{}ftp\+\_\+client.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}cmd\+\_\+client.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}config.\+h\char`\"{}}\newline
{\ttfamily \#include $<$arpa/inet.\+h$>$}\newline
{\ttfamily \#include $<$time.\+h$>$}\newline
{\ttfamily \#include $<$ctype.\+h$>$}\newline
{\ttfamily \#include $<$dirent.\+h$>$}\newline
{\ttfamily \#include $<$errno.\+h$>$}\newline
{\ttfamily \#include $<$fcntl.\+h$>$}\newline
{\ttfamily \#include $<$netdb.\+h$>$}\newline
{\ttfamily \#include $<$netinet/in.\+h$>$}\newline
{\ttfamily \#include $<$stdio.\+h$>$}\newline
{\ttfamily \#include $<$stdlib.\+h$>$}\newline
{\ttfamily \#include $<$string.\+h$>$}\newline
{\ttfamily \#include $<$strings.\+h$>$}\newline
{\ttfamily \#include $<$sys/socket.\+h$>$}\newline
{\ttfamily \#include $<$sys/stat.\+h$>$}\newline
{\ttfamily \#include $<$sys/time.\+h$>$}\newline
{\ttfamily \#include $<$sys/types.\+h$>$}\newline
{\ttfamily \#include $<$termios.\+h$>$}\newline
{\ttfamily \#include $<$unistd.\+h$>$}\newline
Include dependency graph for ftp\+\_\+client.\+c\+:
\nopagebreak
\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=350pt]{ftp__client_8c__incl}
\end{center}
\end{figure}
\doxysubsection*{Macros}
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{ftp__client_8c_a8dc414d1afe7fc1bbdd1c06d682ba7f3}\label{ftp__client_8c_a8dc414d1afe7fc1bbdd1c06d682ba7f3}} 
\#define {\bfseries DEFAULT\+\_\+\+FTP\+\_\+\+PORT}~21
\item 
\mbox{\Hypertarget{ftp__client_8c_aa9e26136af09184e7b167db6e01d6e8e}\label{ftp__client_8c_aa9e26136af09184e7b167db6e01d6e8e}} 
\#define {\bfseries RCV\+\_\+\+BUF\+\_\+\+MAX\+\_\+\+LEN}~510
\end{DoxyCompactItemize}
\doxysubsection*{Functions}
\begin{DoxyCompactItemize}
\item 
void \mbox{\hyperlink{ftp__client_8c_ae82ce0c2ad9bca58caa399c290effbca}{cmd\+\_\+err\+\_\+exit}} (char $\ast$err\+\_\+msg, int err\+\_\+code)
\begin{DoxyCompactList}\small\item\em 打印报错信息并退出。 \end{DoxyCompactList}\item 
\mbox{\Hypertarget{ftp__client_8c_a50af3c8dbaa13d610ab72731646b8001}\label{ftp__client_8c_a50af3c8dbaa13d610ab72731646b8001}} 
void {\bfseries echo\+\_\+off} (void)
\begin{DoxyCompactList}\small\item\em 关闭显示输入字符（用于输入密码） \end{DoxyCompactList}\item 
\mbox{\Hypertarget{ftp__client_8c_a13c96b2939a5739cc81b955b32a98f4e}\label{ftp__client_8c_a13c96b2939a5739cc81b955b32a98f4e}} 
void {\bfseries echo\+\_\+on} (void)
\begin{DoxyCompactList}\small\item\em 打开显示输入字符 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__client_8c_acc60139a882577df9182dec7cf130a95}{ftp\+\_\+get\+\_\+reply}} (int sock\+\_\+fd)
\begin{DoxyCompactList}\small\item\em 接受来自服务端的回应。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__client_8c_a3d3917e46b692a63525f64f0bb1c975d}{ftp\+\_\+send\+\_\+cmd}} (const char $\ast$s1, const char $\ast$s2, int sock\+\_\+fd)
\begin{DoxyCompactList}\small\item\em 拼接给出的两个参数后向客户端发送。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__client_8c_a2da1039d0b9c00763143e93f44ca7416}{xconnect}} (struct sockaddr\+\_\+in $\ast$s\+\_\+addr)
\begin{DoxyCompactList}\small\item\em 连接到服务器。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__client_8c_a0d2d0ee5a8e9a4e4a5f215b52d7e73c2}{rand\+\_\+local\+\_\+port}} ()
\begin{DoxyCompactList}\small\item\em PORT模式下，随机生成一个端口号。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__client_8c_ac95cce0b3a2123cc408859183557ab80}{get\+\_\+port}} ()
\begin{DoxyCompactList}\small\item\em pasv模式下，向server发送\+PASV指令，接收server发回的端口号（第一次握手 \end{DoxyCompactList}\item 
void \mbox{\hyperlink{ftp__client_8c_a007aca7a47d99c72aea9dac4faa64ce9}{xconnect\+\_\+ftpdata}} (\mbox{\hyperlink{structMsg}{Msg}} $\ast$msg)
\begin{DoxyCompactList}\small\item\em 建立数据连接，被动模式下，从server获得端口号，通过server打开的这个端口与server建立数据连接;主动模式下，client指定本地端口号后，向server发\+PORT指令，然后建立一个新的数据连接。 \end{DoxyCompactList}\item 
\mbox{\Hypertarget{ftp__client_8c_a6a1e56fcb9d9f48e879662a60d9eab4d}\label{ftp__client_8c_a6a1e56fcb9d9f48e879662a60d9eab4d}} 
void {\bfseries get\+\_\+user} ()
\begin{DoxyCompactList}\small\item\em 获取用户名。 \end{DoxyCompactList}\item 
\mbox{\Hypertarget{ftp__client_8c_a5eba4ccb7f43b916606b7a703782e6ce}\label{ftp__client_8c_a5eba4ccb7f43b916606b7a703782e6ce}} 
void {\bfseries get\+\_\+pass} ()
\begin{DoxyCompactList}\small\item\em 获取密码。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__client_8c_a048633927810eee4b247c8a43cbb40bf}{ftp\+\_\+login}} ()
\begin{DoxyCompactList}\small\item\em 客户端登录交互。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__client_8c_a6c6decfc6f699c73ab480c3f91882794}{fill\+\_\+host\+\_\+addr}} (char $\ast$host\+\_\+ip\+\_\+addr, struct sockaddr\+\_\+in $\ast$host, int port)
\begin{DoxyCompactList}\small\item\em 设置客户端的sockaddr\+\_\+in。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__client_8c_a710ede996572b54a3e6a014f5f842ecb}{ftp\+\_\+user\+\_\+cmd}} (char $\ast$user\+\_\+cmd)
\begin{DoxyCompactList}\small\item\em 格式化指令,对错误指令返回\+ERROR flag \end{DoxyCompactList}\item 
\mbox{\Hypertarget{ftp__client_8c_a2ae066576c5647d286a8033faa0ee221}\label{ftp__client_8c_a2ae066576c5647d286a8033faa0ee221}} 
int {\bfseries start\+\_\+ftp\+\_\+cmd} (char $\ast$host\+\_\+ip\+\_\+addr, int port)
\begin{DoxyCompactList}\small\item\em 处理启动之后与用户的交互。 \end{DoxyCompactList}\item 
\mbox{\Hypertarget{ftp__client_8c_a0ddf1224851353fc92bfbff6f499fa97}\label{ftp__client_8c_a0ddf1224851353fc92bfbff6f499fa97}} 
int {\bfseries main} (int argc, char $\ast$argv\mbox{[}$\,$\mbox{]})
\begin{DoxyCompactList}\small\item\em 启动客户端，要求ip地址参数。默认在端口21启动。 \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Variables}
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{ftp__client_8c_a86aba5265147ee71567e55f3c92f9df5}\label{ftp__client_8c_a86aba5265147ee71567e55f3c92f9df5}} 
struct sockaddr\+\_\+in ftp\+\_\+server {\bfseries local\+\_\+host}
\item 
\mbox{\Hypertarget{ftp__client_8c_aff32f3e23f89ed16d3e948b1948a0274}\label{ftp__client_8c_aff32f3e23f89ed16d3e948b1948a0274}} 
struct hostent $\ast$ {\bfseries server\+\_\+hostent}
\item 
\mbox{\Hypertarget{ftp__client_8c_a83ce7994ea94971c6e4b0ed94b53651e}\label{ftp__client_8c_a83ce7994ea94971c6e4b0ed94b53651e}} 
int {\bfseries sock\+\_\+control}
\item 
\mbox{\Hypertarget{ftp__client_8c_a2338ae28f14b9f119ed21868867b17d1}\label{ftp__client_8c_a2338ae28f14b9f119ed21868867b17d1}} 
char {\bfseries user} \mbox{[}64\mbox{]}
\item 
\mbox{\Hypertarget{ftp__client_8c_a0866d4203d1162af18c125c88ed199ce}\label{ftp__client_8c_a0866d4203d1162af18c125c88ed199ce}} 
char {\bfseries passwd} \mbox{[}64\mbox{]}
\item 
\mbox{\Hypertarget{ftp__client_8c_a1ea5d0cb93f22f7d0fdf804bd68c3326}\label{ftp__client_8c_a1ea5d0cb93f22f7d0fdf804bd68c3326}} 
int {\bfseries mode} = PORT
\end{DoxyCompactItemize}


\doxysubsection{Detailed Description}
Code for ftp client, basic connection to a local ftp server, connection host ip\+:127.\+0.\+0.\+1, default port\+:21.

\begin{DoxyAuthor}{Author}
ssr 
\end{DoxyAuthor}
\begin{DoxyVersion}{Version}
V1.\+0 
\end{DoxyVersion}
\begin{DoxyDate}{Date}
2022-\/11-\/6 
\end{DoxyDate}
\begin{DoxyCopyright}{Copyright}
GNU Public License V3.\+0 
\end{DoxyCopyright}


\doxysubsection{Function Documentation}
\mbox{\Hypertarget{ftp__client_8c_ae82ce0c2ad9bca58caa399c290effbca}\label{ftp__client_8c_ae82ce0c2ad9bca58caa399c290effbca}} 
\index{ftp\_client.c@{ftp\_client.c}!cmd\_err\_exit@{cmd\_err\_exit}}
\index{cmd\_err\_exit@{cmd\_err\_exit}!ftp\_client.c@{ftp\_client.c}}
\doxysubsubsection{\texorpdfstring{cmd\_err\_exit()}{cmd\_err\_exit()}}
{\footnotesize\ttfamily void cmd\+\_\+err\+\_\+exit (\begin{DoxyParamCaption}\item[{char $\ast$}]{err\+\_\+msg,  }\item[{int}]{err\+\_\+code }\end{DoxyParamCaption})}



打印报错信息并退出。 


\begin{DoxyParams}{Parameters}
{\em err\+\_\+msg} & \\
\hline
{\em err\+\_\+code} & \\
\hline
\end{DoxyParams}
\mbox{\Hypertarget{ftp__client_8c_a6c6decfc6f699c73ab480c3f91882794}\label{ftp__client_8c_a6c6decfc6f699c73ab480c3f91882794}} 
\index{ftp\_client.c@{ftp\_client.c}!fill\_host\_addr@{fill\_host\_addr}}
\index{fill\_host\_addr@{fill\_host\_addr}!ftp\_client.c@{ftp\_client.c}}
\doxysubsubsection{\texorpdfstring{fill\_host\_addr()}{fill\_host\_addr()}}
{\footnotesize\ttfamily int fill\+\_\+host\+\_\+addr (\begin{DoxyParamCaption}\item[{char $\ast$}]{host\+\_\+ip\+\_\+addr,  }\item[{struct sockaddr\+\_\+in $\ast$}]{host,  }\item[{int}]{port }\end{DoxyParamCaption})}



设置客户端的sockaddr\+\_\+in。 

\begin{DoxyReturn}{Returns}
成功则返回1,失败则返回错误码。 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__client_8c_acc60139a882577df9182dec7cf130a95}\label{ftp__client_8c_acc60139a882577df9182dec7cf130a95}} 
\index{ftp\_client.c@{ftp\_client.c}!ftp\_get\_reply@{ftp\_get\_reply}}
\index{ftp\_get\_reply@{ftp\_get\_reply}!ftp\_client.c@{ftp\_client.c}}
\doxysubsubsection{\texorpdfstring{ftp\_get\_reply()}{ftp\_get\_reply()}}
{\footnotesize\ttfamily int ftp\+\_\+get\+\_\+reply (\begin{DoxyParamCaption}\item[{int}]{sock\+\_\+fd }\end{DoxyParamCaption})}



接受来自服务端的回应。 


\begin{DoxyParams}{Parameters}
{\em sock\+\_\+fd} & 连接服务端的套接字。 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
若输入为空，返回0；否则返回对应的reply\+\_\+code数字。 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__client_8c_a048633927810eee4b247c8a43cbb40bf}\label{ftp__client_8c_a048633927810eee4b247c8a43cbb40bf}} 
\index{ftp\_client.c@{ftp\_client.c}!ftp\_login@{ftp\_login}}
\index{ftp\_login@{ftp\_login}!ftp\_client.c@{ftp\_client.c}}
\doxysubsubsection{\texorpdfstring{ftp\_login()}{ftp\_login()}}
{\footnotesize\ttfamily int ftp\+\_\+login (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



客户端登录交互。 

\begin{DoxyReturn}{Returns}
登录成功则返回1,否则返回0。 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__client_8c_a3d3917e46b692a63525f64f0bb1c975d}\label{ftp__client_8c_a3d3917e46b692a63525f64f0bb1c975d}} 
\index{ftp\_client.c@{ftp\_client.c}!ftp\_send\_cmd@{ftp\_send\_cmd}}
\index{ftp\_send\_cmd@{ftp\_send\_cmd}!ftp\_client.c@{ftp\_client.c}}
\doxysubsubsection{\texorpdfstring{ftp\_send\_cmd()}{ftp\_send\_cmd()}}
{\footnotesize\ttfamily int ftp\+\_\+send\+\_\+cmd (\begin{DoxyParamCaption}\item[{const char $\ast$}]{s1,  }\item[{const char $\ast$}]{s2,  }\item[{int}]{sock\+\_\+fd }\end{DoxyParamCaption})}



拼接给出的两个参数后向客户端发送。 

\begin{DoxyReturn}{Returns}
成功则返回发送的字符数，否则返回-\/1。 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__client_8c_a710ede996572b54a3e6a014f5f842ecb}\label{ftp__client_8c_a710ede996572b54a3e6a014f5f842ecb}} 
\index{ftp\_client.c@{ftp\_client.c}!ftp\_user\_cmd@{ftp\_user\_cmd}}
\index{ftp\_user\_cmd@{ftp\_user\_cmd}!ftp\_client.c@{ftp\_client.c}}
\doxysubsubsection{\texorpdfstring{ftp\_user\_cmd()}{ftp\_user\_cmd()}}
{\footnotesize\ttfamily int ftp\+\_\+user\+\_\+cmd (\begin{DoxyParamCaption}\item[{char $\ast$}]{user\+\_\+cmd }\end{DoxyParamCaption})}



格式化指令,对错误指令返回\+ERROR flag 


\begin{DoxyParams}{Parameters}
{\em user\+\_\+cmd} & 用户输入的指令 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
cmd\+\_\+flag，用于指示调用何种功能函数完成指令 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__client_8c_ac95cce0b3a2123cc408859183557ab80}\label{ftp__client_8c_ac95cce0b3a2123cc408859183557ab80}} 
\index{ftp\_client.c@{ftp\_client.c}!get\_port@{get\_port}}
\index{get\_port@{get\_port}!ftp\_client.c@{ftp\_client.c}}
\doxysubsubsection{\texorpdfstring{get\_port()}{get\_port()}}
{\footnotesize\ttfamily int get\+\_\+port (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



pasv模式下，向server发送\+PASV指令，接收server发回的端口号（第一次握手 

\begin{DoxyReturn}{Returns}
若执行成功，返回server打开的端口号；否则返回0。 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__client_8c_a0d2d0ee5a8e9a4e4a5f215b52d7e73c2}\label{ftp__client_8c_a0d2d0ee5a8e9a4e4a5f215b52d7e73c2}} 
\index{ftp\_client.c@{ftp\_client.c}!rand\_local\_port@{rand\_local\_port}}
\index{rand\_local\_port@{rand\_local\_port}!ftp\_client.c@{ftp\_client.c}}
\doxysubsubsection{\texorpdfstring{rand\_local\_port()}{rand\_local\_port()}}
{\footnotesize\ttfamily int rand\+\_\+local\+\_\+port (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



PORT模式下，随机生成一个端口号。 

\begin{DoxyReturn}{Returns}
生成的端口号 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__client_8c_a2da1039d0b9c00763143e93f44ca7416}\label{ftp__client_8c_a2da1039d0b9c00763143e93f44ca7416}} 
\index{ftp\_client.c@{ftp\_client.c}!xconnect@{xconnect}}
\index{xconnect@{xconnect}!ftp\_client.c@{ftp\_client.c}}
\doxysubsubsection{\texorpdfstring{xconnect()}{xconnect()}}
{\footnotesize\ttfamily int xconnect (\begin{DoxyParamCaption}\item[{struct sockaddr\+\_\+in $\ast$}]{s\+\_\+addr }\end{DoxyParamCaption})}



连接到服务器。 


\begin{DoxyParams}{Parameters}
{\em s\+\_\+addr} & socket连接的地址 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
成功则返回所用套接字的描述符，否则返回-\/1。 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__client_8c_a007aca7a47d99c72aea9dac4faa64ce9}\label{ftp__client_8c_a007aca7a47d99c72aea9dac4faa64ce9}} 
\index{ftp\_client.c@{ftp\_client.c}!xconnect\_ftpdata@{xconnect\_ftpdata}}
\index{xconnect\_ftpdata@{xconnect\_ftpdata}!ftp\_client.c@{ftp\_client.c}}
\doxysubsubsection{\texorpdfstring{xconnect\_ftpdata()}{xconnect\_ftpdata()}}
{\footnotesize\ttfamily void xconnect\+\_\+ftpdata (\begin{DoxyParamCaption}\item[{\mbox{\hyperlink{structMsg}{Msg}} $\ast$}]{msg }\end{DoxyParamCaption})}



建立数据连接，被动模式下，从server获得端口号，通过server打开的这个端口与server建立数据连接;主动模式下，client指定本地端口号后，向server发\+PORT指令，然后建立一个新的数据连接。 


\begin{DoxyParams}{Parameters}
{\em msg} & 此函数负责填充msg结构体中的ftp\+\_\+data\+\_\+sock \\
\hline
\end{DoxyParams}
